package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsIssueMapper;
import com.shop.pojo.DtsCoupon;
import com.shop.pojo.DtsIssue;
import com.shop.service.DtsIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsIssueServiceImpl
 * @Author xiao宋
 * @Date 16:27  2022/10/22
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsIssueServiceImpl implements DtsIssueService {
    @Autowired
    DtsIssueMapper issueMapper;
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String question) {
        QueryWrapper<DtsIssue> w = new QueryWrapper<>();
        IPage<DtsIssue> iPage = new Page<>(page, limit);

        if (!StringUtils.isEmpty(question)) {
            w.like("question", question);
        }
//        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        w.eq("deleted", false);
        issueMapper.selectPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }

    @Override
    public void delete(DtsIssue issue) {
        issue.setUpdateTime(new Date());
        issue.setDeleted(true);
        issueMapper.updateById(issue);
    }

    @Override
    public void update(DtsIssue issue) {
        issue.setUpdateTime(new Date());
        issueMapper.updateById(issue);
    }

    @Override
    public DtsIssue insert(DtsIssue issue) {
        issue.setUpdateTime(new Date());
        issue.setAddTime(new Date());
        issueMapper.insert(issue);
        return issue;
    }
}
